package com.mf.ergate.common.exception;

import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 业务异常处理类
 * User: zhaoming
 * Date: 2017-08-24
 * To change this template use File | Settings | File Templates.
 */
@ControllerAdvice
public class BusinessExceptionHandler {

	//日志
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 默认异常处理
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result defaultErrorHandler(Exception e){
		logger.error(e.getMessage(), e);
		return new Result(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
	}

	/**
	 * 自定义业务异常处理
	 */
	@ExceptionHandler(value = BusinessException.class)
	@ResponseBody
	public Result businessErrorHandler(BusinessException e){
		logger.error(e.getMessage(), e);
		return new Result(e.getStatusCode(), e.getErrorCode(), e.getMessage());
	}
}
